package cn.smbms.service.provider;


import java.util.List;

import cn.smbms.mapper.supplier.SupplierMapper;
import cn.smbms.pojo.Provider;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Transactional(rollbackFor = {Exception.class})
@Service("providerService")
public class ProviderServiceImpl implements ProviderService {
	@Resource
	private SupplierMapper supplierMapper;

	@Override
	public boolean add(Provider provider) {
		// TODO Auto-generated method stub
		boolean flag = false;
/*		try {

			sqlSession = MyBatisUtil.createSqlSession();
			int add = sqlSession.getMapper(SupplierMapper.class).add(provider);
			if(add>0){
				flag = true;
				sqlSession.commit();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			sqlSession.rollback();
		}finally{
			MyBatisUtil.closeSqlSession(sqlSession);
		}*/

		if(supplierMapper.add(provider)>0) {
			flag = true;
			//int a = 10/0;
		}
		return flag;
	}

	@Override
	public List<Provider> getProviderList(String proName,String proCode,int currentPageNo,int pageSize) {
		// TODO Auto-generated method stub
		List<Provider> providerList = null;
		System.out.println("query proName ---- > " + proName);
		System.out.println("query proCode ---- > " + proCode);
		/*try {
			sqlSession = MyBatisUtil.createSqlSession();
			SupplierMapper supplierMapper = sqlSession.getMapper(SupplierMapper.class);
			int page=(currentPageNo-1)*pageSize;

			providerList = supplierMapper.getProviderList(proName,proCode,page,pageSize);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			MyBatisUtil.closeSqlSession(sqlSession);
		}*/
		/*
		try {
				providerList =  supplierMapper.getProviderList(proName,proCode,currentPageNo,pageSize);
			}catch (Exception e){
				e.printStackTrace();
			}

		*/
		providerList =  supplierMapper.getProviderList(proName,proCode,currentPageNo,pageSize);
		return providerList;
	}

	/**
	 * 业务：根据ID删除供应商表的数据之前，需要先去订单表里进行查询操作
	 * 若订单表中无该供应商的订单数据，则可以删除
	 * 若有该供应商的订单数据，则不可以删除
	 * 返回值billCount
	 * 1> billCount == 0  删除---1 成功 （0） 2 不成功 （-1）
	 * 2> billCount > 0    不能删除 查询成功（0）查询不成功（-1）
	 * 
	 * ---判断
	 * 如果billCount = -1 失败
	 * 若billCount >= 0 成功
	 */
	@Override
	public int deleteProviderById(String delId) {
		// TODO Auto-generated method stub

		/*try {
			sqlSession = MyBatisUtil.createSqlSession();
			billCount = sqlSession.getMapper(StorageRecordMapper.class).getBillCountByProviderId(delId);
			if (billCount==0){
				sqlSession.getMapper(SupplierMapper.class).deleteProviderById(delId);
			}
			sqlSession.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			billCount = -1;
			sqlSession.rollback();
		}finally{
			MyBatisUtil.closeSqlSession(sqlSession);
		}*/
		return supplierMapper.deleteProviderById(delId);
	}

	@Override
	public Provider getProviderById(String id) {
		// TODO Auto-generated method stub
		Provider providerById = supplierMapper.getProviderById(id);
		/*try{
			sqlSession = MyBatisUtil.createSqlSession();
			providerById = sqlSession.getMapper(SupplierMapper.class).getProviderById(id);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();

		}finally{
			MyBatisUtil.closeSqlSession(sqlSession);
		}*/
		return providerById;
	}

	@Override
	public boolean modify(Provider provider) {
		// TODO Auto-generated method stub
		boolean flag = false;
/*		try {
			sqlSession = MyBatisUtil.createSqlSession();
			int result = sqlSession.getMapper(SupplierMapper.class).modify(provider);
			if(result>0){
				sqlSession.commit();
				flag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		}finally{
			MyBatisUtil.closeSqlSession(sqlSession);
		}*/
		if(supplierMapper.modify(provider)>0){
			flag = true;
		}
		return flag;
	}

	@Override
	public int count(String proName, String proCode) {
		// TODO Auto-generated method stub
		int count = 0;
		System.out.println("proName ---- > " + proName);
		System.out.println("proCode ---- > " + proCode);
		/*try {
			sqlSession = MyBatisUtil.createSqlSession();
			count =sqlSession.getMapper(SupplierMapper.class).findAllTotalCount(proName,proCode);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			MyBatisUtil.closeSqlSession(sqlSession);
		}*/
		try {
			count = supplierMapper.findAllTotalCount(proName, proCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}

}
